<?php
class Payment extends CS_Controller
{
	
	public function _init()
	{
		$this->load->helper('common');
        $this->load->library('alipay/alipaypc', null, 'alipaypc');
        $this->load->model('pay_failure_log_model', 'pay_failure_log');
        $this->load->model('pay_method_model', 'pay_method');
        $this->load->model('user_account_model', 'user_account');
        $this->load->model('account_log_model', 'account_log');
        $this->load->model('deposit_model', 'deposit');
	}

    
    /**
     * 支付宝支付回调函数。
     */
    public function alipayReturn()
    {
        $response = $this->alipaypc->responseAlipayReturn();
        $depositId = $this->input->get('out_trade_no', true);
        $getDate = $this->input->get();
        //验证是否支付
        $depositInfo = $this->_depositIsCorrect($depositId);

        if ($response) { //$response付款成功；
            $this->db->trans_begin();
            $userAccount = $this->user_account->findByUid($depositInfo->uid)->row();
            
            //更新充值状态 增加用户可提现金额
            $depositData = array('amount_carry' => $userAccount->amount_carry, 'state' => 2);
            $state = $this->deposit->updateState($depositInfo->deposit_id, $depositInfo->uid, $depositData);
            $account = $this->user_account->userDeposit($depositInfo->uid, $depositInfo->amount);
            $accountLog = $this->account_log->insertAccountLogDeposit($depositInfo, $userAccount->amount_carry);
            if ( ! $state || ! $account || ! $accountLog || $this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                $this->redirect('home/account/fail');
            } else {
                $this->db->trans_commit();
                $this->redirect('home/account/complete');
            }
        } else {
            $result = $this->pay_failure_log->findByOrderSn($depositId);
            if (!$result) { //保存错误日志。
                $last_id = $this->pay_failure_log->insertPayFailureLog($depositId, $getDate, $response);
            }
            $this->redirect('home/account/fail');
        }
    }

    /**
     * 微信支付
     */
    public function wxCompletePay($depositId)
    {
        $err_msg = $this->input->get('err_msg');
        if ($err_msg && $err_msg == 'get_brand_wcpay_request:ok') {
        	$result = $this->deposit->findById($depositId);
            if ($result->num_rows() == 0)
            {
                $this->alertJumpPre('充值异常');
            }
            $depositInfo = $result->row();
            if ($depositInfo->state == 2)//如果支付过
            {
                $this->redirect('home/account/complete');
            } else {
                $this->redirect('home/account/fail');
            }
	        
        } else {
            $this->redirect('home/account/fail');
        }
    }

    /**
     * 充值信息是否正确
     */
    private function _depositIsCorrect($id)
    {
        if ( empty($id) )
        {
            $this->alertJumpPre('充值异常');
        }
        $result = $this->deposit->findById($id);
        if ($result->num_rows() == 0)
        {
            $this->alertJumpPre('充值异常');
        }
        $depositInfo = $result->row();
        if ($depositInfo->state == 2)//如果支付过
        {
            $this->redirect('home/account/complete');
        }
        return $depositInfo;
    }
}